package org.jackysoft.web;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.User;
import org.jackysoft.ireporter.IReporterTool;
import org.jackysoft.util.SearchUtils;
import org.jackysoft.util.SessionKey;
import org.jackysoft.util.SpringSecurityUtils;

/**
 * Servlet implementation class AppConfig
 */
public class AppConfig implements Filter {

	private static final Log log = LogFactory.getLog(AppConfig.class);
	private static final String APP_PROPERTIES = "app.properties";
	private static String sbase = null;
	private static String logouturl = null;
	private static String useSystem = "true";
	
	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain fc) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
			
		HttpSession session = request.getSession();
		
		Object skey = session.getAttribute(SessionKey.HTTP_BASE_PATH);
		
		if(skey!=null){
			fc.doFilter(arg0, arg1);
			return;
		}
		log.info("appconfig uri :" + request.getRequestURL());

		String path = request.getContextPath();
		String httpBase = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";

		String basePath = request.getRealPath("");
		session.setAttribute(SessionKey.HTTP_BASE_PATH, httpBase);

		User user = SpringSecurityUtils.getCurrentUser();
		if(user==null){
			fc.doFilter(arg0, arg1);
			return;
		}
		session.setAttribute("loginUser", user);
		Boolean sysed = useSystem==null?true:Boolean.parseBoolean(useSystem);
		log.info("设置系统属性:"+sysed+","+useSystem);
		if (sysed) {
			Properties props = System.getProperties();
			props.setProperty("appBase", basePath);
			props.setProperty("sbase", sbase);
			props.setProperty("base", httpBase);			
			props.setProperty("logouturl", logouturl);
			
		}
		
		session.setAttribute("appContext", path);
		
		log.info("application context path is : "+path);	

		session.setAttribute("sbase", sbase);
		log.info("static uri is :" + sbase);		

		session.setAttribute("logouturl", logouturl);
		log.info("logout url is :" + logouturl);

		session.setAttribute("appBase", basePath);
		log.info(" appBase is :" + basePath);
		
		fc.doFilter(arg0, arg1);

	}

	@Override
	public void init(FilterConfig fc) throws ServletException {
		useSystem = fc.getInitParameter("useSystem");
		try {

			Configuration config = new PropertiesConfiguration(APP_PROPERTIES);
			sbase = config.getString("sbase");
		    logouturl = config.getString("logouturl");			
		} catch (ConfigurationException e) {
			log.error(e);
		}

		File f = new File(fc.getServletContext().getRealPath("/jasper/"));
		if (f.exists()) {
			IReporterTool.compileReport(f);
			SearchUtils.indexAll(fc.getServletContext());
		}

	}

}
